package com.caishi.lkx.order.pay;

import com.caishi.lkx.order.ienum.type.PaymentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author by keray
 * date:2019/9/9 11:54
 */
@Configuration
public class PayFactory {

    @Resource
    private OfflinePay offlinePay;
    @Resource
    private AgentPay agentPay;
    @Autowired(required = false)
    private ZFBPayPlugins zfbPayPlugins;

    @Autowired(required = false)
    private WeiXinPayPlugins weiXinPayPlugins;
    @Resource
    private UnionPayPlugins unionPayPlugins;
    @Resource
    private CodePay codePay;

    @Resource
    private DefaultPay defaultPay;

    private static PayFactory factory;

    @PostConstruct
    public void init() {
        factory = this;
    }

    public static PayPlugins create(PaymentType type) {
        if (type == PaymentType.alipay) {
            if (factory.zfbPayPlugins != null && factory.zfbPayPlugins.open()) return factory.zfbPayPlugins;
        } else if (type == PaymentType.weixin) {
            if (factory.weiXinPayPlugins != null && factory.weiXinPayPlugins.open()) return factory.weiXinPayPlugins;
        } else if (type == PaymentType.activationCode) {
            return factory.codePay;
        } else if (type == PaymentType.agentpay) {
            return factory.agentPay;
        } else if (type == PaymentType.offline) {
            return factory.offlinePay;
        }else if(type == PaymentType.unionpay){
            return  factory.unionPayPlugins;
        }
        return factory.defaultPay;
    }
}
